//
//  YJPublishSelectImageCell.m
//  YaJi
//
//  Created by 牙记 on 2018/12/27.
//  Copyright © 2018 牙衫科技. All rights reserved.
//

#import "YJPublishSelectImageCell.h"
#import "YJPublishSelectImageCollViewCell.h"

#define KYJPublishSelectImageSize ((kYQPScreenWidth-24-20) / 3.0)


@interface YJPublishSelectImageCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collView;

@end

@implementation YJPublishSelectImageCell
-(void)setCollImageArr:(NSArray *)collImageArr {
    _collImageArr = [collImageArr copy];
    [self.collView reloadData];
}
-(void)setAssetArr:(NSArray *)assetArr {
    _assetArr = [assetArr copy];
}
#pragma mark - laze
- (UICollectionView *)collView {
    if (!_collView) {
        UICollectionViewFlowLayout *collViewLayout = [[UICollectionViewFlowLayout alloc] init];
        collViewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        collViewLayout.minimumLineSpacing= 10;
        collViewLayout.minimumInteritemSpacing= 5;
        
        collViewLayout.itemSize = CGSizeMake(KYJPublishSelectImageSize, KYJPublishSelectImageSize);
        
        _collView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collViewLayout];
        
        _collView.dataSource = self;
        _collView.delegate = self;
        _collView.showsHorizontalScrollIndicator = NO;
        _collView.backgroundColor = [UIColor whiteColor];
        _collView.bounces = NO;
        
        [_collView registerNib:[UINib nibWithNibName:@"YJPublishSelectImageCollViewCell" bundle:nil] forCellWithReuseIdentifier:@"YJPublishSelectImageCollViewCell"];
    }
    return _collView;
}
#pragma mark - laze end

#pragma mark -- <UICollectionViewDelegate, UICollectionViewDataSource>
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.collImageArr.count > 6) {
        return 6;
    }
    return self.collImageArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    YJPublishSelectImageCollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YJPublishSelectImageCollViewCell" forIndexPath:indexPath];
    UIImage *image = nil;
    if (indexPath.item < self.collImageArr.count) {
        image = self.collImageArr[indexPath.item];
    }
    cell.publishImageView.image = image;
    
    if (indexPath.item == self.collImageArr.count - 1 && self.collImageArr.count <= 6) {
        if (self.collImageArr.lastObject == self.tempAddImag) {
            cell.deleteBtn.hidden = YES;
        }else{
            cell.deleteBtn.hidden = NO;
        }
    }else{
        cell.deleteBtn.hidden = NO;
    }
    

    
//    WeakSelf;
//    [cell.deleteBtn addTapAction:^(id sender) {
//
//    }];
    cell.deleteBtn.tag = 11000 + indexPath.item;
    [cell.deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.item == self.collImageArr.count - 1 && self.collImageArr.count <= 6) {
        if ([self.delegate respondsToSelector:@selector(publishSelectionImageIndexPath:)]) {
            [self.delegate publishSelectionImageIndexPath:self.indexPath];
        }
    }else{
        if (self.assetArr.count == 0) {
            return;
        }

    }
    
    
}
#pragma mark -- <UICollectionViewDelegate, UICollectionViewDataSource>
-(void)deleteAction:(UIButton *)sender {
    NSInteger item = sender.tag - 11000;
    if ([self.delegate respondsToSelector:@selector(deleteActionIndexPath:tableViewIndexPath:)]) {
        [self.delegate deleteActionIndexPath:[NSIndexPath indexPathForItem:item inSection:0] tableViewIndexPath:self.indexPath];
    }
}
-(void)layoutSubviews {
    [super layoutSubviews];
    
    [self.contentView addSubview:self.collView];
    [self.collView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-12);
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
    }];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
